{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "from math import sin, cos, pi\n",
    "\n",
    "width = height = length = 20\n",
    "model_matrix = [\n",
    "    7.0,  5.0, -5.0, 0.0,\n",
    "    0.0,  7.0,  7.0, 5.0,\n",
    "    7.0, -5.0,  5.0, 0.0,\n",
    "    0.0,  0.0,  0.0, 1.0\n",
    "]\n",
    "\n",
    "def f(x, y, z):\n",
    "    return sin(float(x) / width * pi * 2.0), cos(float(y) / height * pi * 2.0), sin(float(z) / length * pi * 2.0)\n",
    "\n",
    "colors = (0xFF0000, 0x00FF00) * width * height * length\n",
    "vectors = [[[f(x, y, z) for x in range(width)] for y in range(height)] for z in range(length)]\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot += k3d.vector_field(vectors, colors, model_matrix=model_matrix, use_head=False)\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].scale = 10.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].use_head = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#credits:https://github.com/Davide-sd\n",
    "\n",
    "import numpy as np \n",
    "\n",
    "fig = k3d.plot()\n",
    "n = 20\n",
    "x = np.linspace(-5, 5, n)\n",
    "y = np.linspace(-5, 5, n)\n",
    "z = np.linspace(-5, 5, n)\n",
    "xx, yy, zz = np.meshgrid(x, y, z)\n",
    "uu, vv, ww = zz, yy, xx\n",
    "xx, yy, zz, uu, vv, ww = [t.flatten().astype(np.float32) for t in [xx, yy, zz, uu, vv, ww]]\n",
    "scale = 0.5\n",
    "magnitude = np.sqrt(uu**2 + vv**2 + ww**2)\n",
    "vectors = np.array((uu, vv, ww)).T * scale\n",
    "origins = np.array((xx, yy, zz)).T\n",
    "colors = k3d.helpers.map_colors(magnitude, k3d.matplotlib_color_maps.Plasma, [])\n",
    "vec_colors = np.zeros(2 * len(colors))\n",
    "for i, c in enumerate(colors):\n",
    "    vec_colors[2 * i] = c\n",
    "    vec_colors[2 * i + 1] = c\n",
    "vec_colors = vec_colors.astype(np.uint32)\n",
    "vec = k3d.vectors(\n",
    "    origins = origins - vectors / 2,\n",
    "    vectors = vectors,\n",
    "    colors = vec_colors,\n",
    ")\n",
    "fig += vec\n",
    "fig.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
